

<template>
  <div class="basic-layout">
    <!--导航栏 -->
    <div :class="['nav-side', isCollapse ? 'fold' : 'unfold']">
      <div class="log">
        <!-- 导航 -->
        <img src="../assets/logo.png" alt="" class="logoImage" />
        <span class="nav-side-title">管理后台</span>
      </div>
      <!-- 导航 -->
      <el-menu
          :default-active="activeMenu"
          background-color="#001529"
          text-color="#fff"
          :collapse=isCollapse
          router="true"
          class="nav-menu"
          @open="handleOpen"
          @close="handleClose"
      >
        <tree-menu :userMenu="menuList"/>
      </el-menu>
    </div>

    <!-- 右侧主界面 -->
    <div :class="['content-right', isCollapse ? 'fold' : 'unfold']">
      <!-- 主界面顶部 -->
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="toggle">
            <el-icon v-if=!this.isCollapse><ArrowLeft /></el-icon>
            <el-icon v-else><ArrowRight /></el-icon>
          </div>
          <div class="bread">面包屑</div>
        </div>

        <div class="user-info">
          <el-icon class="user-info-icon"><Bell /></el-icon>
          <el-dropdown @command="handleCommand">
            <span class="user-link">
              {{ userInfo.userName }}
              <el-icon class="el-icon--right"> </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email"
                  >邮箱：{{ userInfo.userEmail }}</el-dropdown-item
                >
                <el-dropdown-item command="quit">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <!-- 实际页面 -->
      <div class="wrapper">
        <div class="main-page">
          <router-view></router-view>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import TreeMenu from "./TreeMenu.vue";
export default {
  name: "主页",
  components:{TreeMenu},
  data() {
    return {
      isCollapse: false,
      userInfo: this.$store.state.userInfo,
      menuList:[],
      activeMenu:location.hash.slice(1)
    };
  },
  mounted() {
    // this.getNoticeCount();
    this.getMenuList();
  },
  methods: {
    handleOpen(){
      console.log("菜单打开")
    },
    handleClose(){
      console.log("菜单关闭")
    },
    handleCommand(key) {
      if (key == "quit") {
        this.$store.commit("saveUserInfo", "");
        this.userInfo = null;
        this.$router.push("/Login");
      }
    },
    async getNoticeCount(){
      try {
        const count  = await this.$apis.getNoticeCount()
        console.log("NoticeCount = ",count)
      }catch (err){
        console.error("err = ",err)
      }

    },
    async getMenuList(){
      try {
        const info = this.$store.state["userInfo"]["token"]
        const list  = await this.$apis.getMenuList(info)
        // 对 Base64 编码的内容解码
        const decodedBase64 = atob(list);
        // 对解码后的内容进行解码
        const decodedChinese =  decodeURIComponent(escape(decodedBase64));
        const jsonArray = JSON.parse(decodedChinese);
        this.menuList = jsonArray;
        console.log("MenuList = ",this.menuList)
      }catch (err){
        console.error("err = ",err)
      }

    },
    toggle() {
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) {
      } else {
      }
    },
  },
};
import { ElMessage } from "element-plus";
</script>

<style lang="scss">
.basic-layout {
  position: relative;
  .nav-side {
    .nav-side-title {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      line-height: 0;
    }
    .log {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;
      .logoImage {
        margin: 0 16px;
        width: 32px;
        height: 32px;
      }
    }

    .nav-menu {
      border-right: none;
    }
    //合并
    &.fold {
      width: 64px;
    }
    &.unfold {
      width: 200px;
    }
    position: fixed;
    width: 200px;
    height: 100vh;
    background-color: #001529;
    color: aliceblue;
    overflow-y: auto;
    transition: width 0.3s;
  }
  .content-right {
    margin-left: 200px;
    //合并
    &.fold {
      margin-left: 64px;
    }
    &.unfold {
      margin-left: 200px;
    }
    .nav-top {
      height: 50px;
      line-height: 50px;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #b4d2ee;
      padding: 0 20px;
      .nav-left {
        display: flex;
        align-items: center;
        justify-content: center;
        .menu-fold {
          margin-right: 15px;
          font-size: 18px;
        }
      }
      .user-info {
        display: flex;
        align-items: center;
        justify-content: center;
        .user-info-icon {
          margin: 0 15px;
          line-height: 30px;
        }
        .user-link {
          cursor: pointer;
          color: #409eff;
        }
      }
    }
    .wrapper {
      background: #b4d2ee;
      padding: 20px;
      height: calc(100vh - 50px);
      .main-page {
        height: 100%;
        background-color: #ffffff;
      }
    }
  }
}
</style>
